package com.heifeng.cloudshelf.mvp.search.presenter;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.RxPresenter;
import com.heifeng.cloudshelf.dao.SearchDefaultBean;
import com.heifeng.cloudshelf.mvp.search.activity.SearchHouseActivity;
import com.heifeng.cloudshelf.mvp.search.adapter.SearchNoDataRvAdapter;
import com.heifeng.cloudshelf.mvp.search.contract.SearchNoDataActivityContract;
import com.heifeng.cloudshelf.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by admin on 2019/9/16.
 */

public class SearchNoDataActivityPresenter extends RxPresenter<SearchNoDataActivityContract.View> implements SearchNoDataActivityContract.Presenter {
    private LinearLayoutManager layoutManager;

    private SearchNoDataRvAdapter adapter;

    private List<SearchDefaultBean> stringList = new ArrayList<>();

    private List<List<SearchDefaultBean>> allList = new ArrayList<>();

    @Inject
    public SearchNoDataActivityPresenter() {
    }

    @Override
    public void init(List<SearchDefaultBean> data) {
        stringList.clear();
        stringList.addAll(data);
        allList = splitList(data, 2);
        initRecyclerView(mView.getRecyclerView());
    }

    public void initRecyclerView(RecyclerView recyclerView) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.width = DensityUtils.dp2px(mContext, 186) * 3 + DensityUtils.dp2px(mContext, 60);
        recyclerView.setLayoutParams(layoutParams);

        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        //gridLayoutManager = new GridLayoutManager(mContext, 3);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new SearchNoDataRvAdapter(mContext, R.layout.layout_search_no_data_rv_item, allList);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchDefaultBean bean = ((SearchDefaultBean) adapter.getItem(position));
                SearchHouseActivity.open(mContext, bean.getId(), bean.getTitle());
            }
        });
        recyclerView.setAdapter(adapter);
    }

    /**
     * @param list
     * @param len
     * @return 按指定大小，分隔集合，将集合按规定个数分为n个部分
     */
    public static List<List<SearchDefaultBean>> splitList(List<SearchDefaultBean> list, int len) {
        if (list == null || list.size() == 0 || len < 1) {
            return new ArrayList<>();
        }
        List<List<SearchDefaultBean>> result = new ArrayList<List<SearchDefaultBean>>();
        int size = list.size();
        int count = (size + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<SearchDefaultBean> subList = new ArrayList<>();
            subList.addAll(list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1))));
            result.add(subList);
        }
        return result;
    }
}
